#include <signal.h>
#include "common.h"
#include "server.h"

SERVER servMa = {0}, servSla = {0};
NET_ADDR addrMa = {3511, "192.168.9.58", ""};
NET_ADDR addrSla = {9999, "192.168.9.58", ""};
SOCK_NODE *head = NULL;

INT32 main(int argc, char **argv)
{
	struct sigaction sa;
	pid_t pid;
	INT32 r;

	sa.sa_handler = SIG_IGN;
	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);

	unlink(NAME_FIFO_1);
	mkfifo(NAME_FIFO_1, 0744);

	unlink(NAME_FIFO_2);
	mkfifo(NAME_FIFO_2, 0744);

	r = sigaction(SIGPIPE, &sa, NULL);
	if(r == -1){
		perror("sigaction().");
		return -1;
	}
	//change
	int logfd = open("logreport", O_CREAT|O_RDWR|O_APPEND|O_SYNC,0644);
	if (logfd != -1)
	{
		close(STDERR_FILENO);
		dup2(logfd, STDERR_FILENO);
	}
	if((pid = fork()) == -1){
		E(" fork error.");
		return -1;
	}
	else if(pid == 0){

		r = server_init(&servSla, &addrSla);
		if(r != 0){
			E(" Fail to server init.");
			return -1;
		}

		D(" Wait platform connect...");
		for(; ;){
			r = server_new_client(&servSla, service_platfThread);
			if(r == -1){
				E(" Fail to server new client.");
				continue;
			}
		}
	}
	else{
		head = link_head_create();
		
		r = server_init(&servMa, &addrMa);
		if(r != 0){
			E(" Fail to server init.");
			return -1;
		}

		D(" Wait terminal connect...");
		for(; ;){
			r = server_new_client(&servMa, service_termThread);
			if(r == -1){
				E(" Fail to server new client.");
				continue;
			}
		}

	}

	return 0;
}
